import React, { Component } from 'react';
import { connect } from 'react-redux';
import {fromJS} from 'immutable';
import { Link } from 'react-router-dom'
import OrderList from './components/orderList';
import { AddressPage } from './style';
import { actionCreators } from './store'
class AddressList extends Component {
    constructor(props) {
        super(props)
        this.state = {
            data: []
        }
    }
    componentDidMount() {
        let { addressList, getAddress } = this.props;
        if (addressList.length === 0) {
            getAddress()
        }
    }
    checked(index, _this) {
        let data = [...this.props.addressList];
        data = data.map((i, tx) => {
            if (index === tx) {
                i.selected = !i.selected;
            } else {
                i.selected = false
            }
            return i
        })
        this.props.changeAllList(data)
    }
    del(index){
        let data = [...this.props.addressList];
        data.splice(index,1)
        this.props.changeAllList(data)
    }
    render() {
        let { addressList } = this.props;
        console.log(11213, addressList)
        return (
            <AddressPage>
                <div className="list">
                    {addressList.map((i, index) => {
                        return <OrderList i={i} key={index} index={index} 
                        del={() => { this.del(index, this) }}
                        checked={() => { this.checked(index, this) }} />
                    })}
                </div>
                <Link to="/addressAdd" className="footer">
                    新增地址
                </Link>
            </AddressPage >
        );
    }
}
const mapStateToProps = (state, ownProps) => {
    state= fromJS(state);
    return {
        addressList: state.getIn(["shopaddress", 'addressList']).toJS()
    };
};
const mapDispatchToProps = (dispatch, ownProps) => {
    return {
        getAddress() {
            dispatch(actionCreators.getAddressList())
        },
        changeAllList(data) {
            dispatch(actionCreators.changeAllList(data))
        }
    };
};
export default connect(mapStateToProps, mapDispatchToProps)(AddressList);
